#### Replication - PSRM ####

## This R Script replicates the analysis for Figure 3, Table 1, and Appendix G
# in "Differential Efficacy of Survey Incentives 
# Across Contexts: Experimental Evidence from Australia, India, and the United States" 


rm(list=ls())

library(readstata13)
library(magrittr)
library(dplyr)
library(ggplot2)
library(ggpubr)
library(scales)
library(tidyr)


#### Load data for all 3 countries #### 
ind_ult <- readstata13::read.dta13("ind_dictator_final.dta")
us <- readstata13::read.dta13("us_dictator_final.dta")
aus <- readstata13::read.dta13("aus_dictator_final.dta")


#### Table 1: Avg. Amount Donated to Charity (USD) ####
### 1.A India ###


# Note: civic7a1 is list of organizations for treatment=1, a2 for treatment=2, etc.; 
# option _12 is "yourself", _1 to _10 are orgs


# Generate Treatment Variables 
ind_ult$treatment <- NA
ind_ult$treatment[ind_ult$treatment1==1] <- 1 
ind_ult$treatment[ind_ult$treatment2==1] <- 2
ind_ult$treatment[ind_ult$treatment3==1] <- 3
ind_ult$treatment[ind_ult$treatment4==1] <- 4
ind_ult$treatment[ind_ult$treatment5==1] <- 5

# Generate the % of individuals who donated everything to charity 

ind_ult$charity_all <- NA

ind_ult$charity_all[ind_ult$civic7a1_12 == 0 & !is.na(ind_ult$civic7a1_12)] <- 1
ind_ult$charity_all[ind_ult$civic7a2_12 == 0 & !is.na(ind_ult$civic7a2_12)] <- 1
ind_ult$charity_all[ind_ult$civic7a3_12 == 0 & !is.na(ind_ult$civic7a3_12)] <- 1
ind_ult$charity_all[ind_ult$civic7a4_12 == 0 & !is.na(ind_ult$civic7a4_12)] <- 1
ind_ult$charity_all[ind_ult$civic7a5_12 == 0 & !is.na(ind_ult$civic7a5_12)] <- 1

ind_ult$charity_all[ind_ult$civic7a1_12 != 0 & !is.na(ind_ult$civic7a1_12)] <- 0
ind_ult$charity_all[ind_ult$civic7a2_12 != 0 & !is.na(ind_ult$civic7a2_12)] <- 0
ind_ult$charity_all[ind_ult$civic7a3_12 != 0 & !is.na(ind_ult$civic7a3_12)] <- 0
ind_ult$charity_all[ind_ult$civic7a4_12 != 0 & !is.na(ind_ult$civic7a4_12)] <- 0
ind_ult$charity_all[ind_ult$civic7a5_12 != 0 & !is.na(ind_ult$civic7a5_12)] <- 0

india_charity_all <- mean(ind_ult$charity_all, na.rm = TRUE)*100



## Generate information on how much each individual donated to charity by treatment

col1 <- which(colnames(ind_ult)=="civic7a1_1")
col2 <- which(colnames(ind_ult)=="civic7a1_10")
ind_ult$charity_amount1 <- ifelse(!is.na(ind_ult$charity_all) & ind_ult$treatment==1, 
                                  rowSums(ind_ult[col1:col2], na.rm =T), NA)


col1 <- which(colnames(ind_ult)=="civic7a2_1")
col2 <- which(colnames(ind_ult)=="civic7a2_10")
ind_ult$charity_amount2 <- ifelse(!is.na(ind_ult$charity_all) & ind_ult$treatment==2, 
                                  rowSums(ind_ult[col1:col2], na.rm =T), NA)

col1 <- which(colnames(ind_ult)=="civic7a3_1")
col2 <- which(colnames(ind_ult)=="civic7a3_10")
ind_ult$charity_amount3 <- ifelse(!is.na(ind_ult$charity_all) & ind_ult$treatment==3, 
                                  rowSums(ind_ult[col1:col2], na.rm =T), NA)

col1 <- which(colnames(ind_ult)=="civic7a4_1")
col2 <- which(colnames(ind_ult)=="civic7a4_10")
ind_ult$charity_amount4 <- ifelse(!is.na(ind_ult$charity_all) & ind_ult$treatment==4, 
                                  rowSums(ind_ult[col1:col2], na.rm =T), NA)

col1 <- which(colnames(ind_ult)=="civic7a5_1")
col2 <- which(colnames(ind_ult)=="civic7a5_10")
ind_ult$charity_amount5 <- ifelse(!is.na(ind_ult$charity_all) & ind_ult$treatment==5, 
                                  rowSums(ind_ult[col1:col2], na.rm =T), NA)

# Generate one column with charity amount based on treatment assignment
ind_ult$charity_amount <- ifelse(ind_ult$treatment==1, ind_ult$charity_amount1, 
                                 ifelse(ind_ult$treatment==2, ind_ult$charity_amount2,
                                        ifelse(ind_ult$treatment==3, ind_ult$charity_amount3,
                                               ifelse(ind_ult$treatment==4, ind_ult$charity_amount4,
                                                      ifelse(ind_ult$treatment==5, ind_ult$charity_amount5,0))))) 




# Generate the % of individuals who donated nothing to charity 

donated_0_ind <- mean(ind_ult$charity_amount==0, na.rm = TRUE)*100


# Generate information on % donated something to charity

donated_ind <- mean(ind_ult$charity_amount>0, na.rm = TRUE)*100 #donated anything to charity

options(digits=2)
ind_summary <- ind_ult %>%
  summarise (Min = min((charity_amount), na.rm = T), 
             Max = max((charity_amount), na.rm=T),
             Median=median((charity_amount), na.rm=T),
             Mean=mean((charity_amount), na.rm=T),
             SD=sd((charity_amount), na.rm=T)) %>% 
  dplyr::mutate_if(is.numeric, round, 3)

ind_summary <- ind_summary %>%
  pivot_longer(everything(), names_to = "Statistic", values_to = "India") #Transpose the table


#Combine all data for India 

ind_summary2 <- data.frame(
  Statistic = c("% Donated All", "% Donated Something", "% Donated None"),
  India = c(india_charity_all, donated_ind, donated_0_ind))

ind_summary <- rbind(ind_summary, ind_summary2)
print(ind_summary, digits=2)





### 1b. Summary Stats for US ###

# Note: civic7a1 is list of organizations; option _12 is "yourself", _1 to _10 are orgs

# Generate the % of individuals who donated everything to charity 

us$charity_all <- ifelse(us$civic7a1_12 == 0 & !is.na(us$civic7a1_12), 1, 
                         ifelse(us$civic7a1_12!=0 & !is.na(us$civic7a1_12),0,NA))

us_charity_all <- mean(us$charity_all, na.rm=TRUE)*100


# Generate the % of individuals who donated nothing to charity 
donated_0_us <- mean(us$civic7a1_12==100, na.rm = TRUE)*100

# Generate information on how much each individual donated to charity, then those who donated something to charity

us$charity_amount <- ifelse(!is.na(us$charity_all), rowSums(us[2:11], na.rm=TRUE),NA)

us$charity_amount <- as.numeric(us$charity_amount)

us$charity_yes <- NA
us$charity_yes <- ifelse(us$charity_amount > 0, 1,
                              ifelse(us$charity_amount==0, 0, NA))

donated_us <- mean(us$charity_yes, na.rm = TRUE)*100 #donated anything to charity


us_summary <- us %>%
  summarise (Min = min((charity_amount), na.rm = T), 
             Max = max((charity_amount), na.rm=T),
             Median=median((charity_amount), na.rm=T),
             Mean=mean((charity_amount), na.rm=T),
             SD=sd((charity_amount), na.rm=T))  %>% 
            dplyr::mutate_if(is.numeric, round, 3)

us_summary <- us_summary %>%
  pivot_longer(everything(), names_to = "Statistic", values_to = "U.S.") #Transpose the table




#Combine all data for US 

us_summary2 <- data.frame(
  Statistic = c("% Donated All", "% Donated Something", "% Donated None"),
  U.S. = c(us_charity_all, donated_us, donated_0_us))

us_summary <- rbind(us_summary, us_summary2)

print(us_summary)



### 1c. Summary Stats for Australia


# Generate the % of individuals who donated everything to charity 

aus$charity_all <- ifelse(aus$civic7a_1_12 == 0 & !is.na(aus$civic7a_1_12), 1, 
                          ifelse(aus$civic7a_1_12!=0 & !is.na(aus$civic7a_1_12),0, NA))

aus_charity_all <- mean(aus$charity_all, na.rm=TRUE)*100

# Generate the % of individuals who donated nothing to charity 
donated_0_aus <- mean(aus$civic7a_1_12==100, na.rm = TRUE)*100


# Generate information on how much each individual donated to charity, then those who donated something to charity

aus$charity_amount <- ifelse(!is.na(aus$charity_all), rowSums(aus[3:12], na.rm=TRUE),NA)

aus$charity_amount <- as.numeric(aus$charity_amount)

aus$charity_yes <- NA
aus$charity_yes <- ifelse(aus$charity_amount > 0, 1,
                         ifelse(aus$charity_amount==0, 0, NA))

donated_aus <- mean(aus$charity_yes, na.rm = TRUE)*100 #donated anything to charity



aus_summary <- aus %>%
  summarise (Min = min((charity_amount), na.rm = T), 
             Max = max((charity_amount), na.rm=T),
             Median=median((charity_amount), na.rm=T),
             Mean=mean((charity_amount), na.rm=T),
             SD=sd((charity_amount), na.rm=T))%>% 
  dplyr::mutate_if(is.numeric, round, 3)

aus_summary <- aus_summary %>%
  pivot_longer(everything(), names_to = "Statistic", values_to = "Australia") #Transpose the table



#Combine all data for Australia

aus_summary2 <- data.frame(
  Statistic = c("% Donated All", "% Donated Something", "% Donated None"),
  Australia = c(aus_charity_all, donated_aus, donated_0_aus))

aus_summary <- rbind(aus_summary, aus_summary2)
print(aus_summary)

### Table 1: Avg. Amount Donated to Charity (USD) ###

tab1 <- cbind(aus_summary, ind_summary, us_summary)

tab1 <- tab1[,c(1,2,4,6)]
table1 <- xtable::xtable(tab1)
print(table1, file="Tables/table1.txt")


#### FIG 3: DICTATOR GAME RESULTS ####

### GGPLOT for India 

library(ggplot2)
library(scales)

indplot <- ggplot(data=ind_ult, aes(x=charity_amount, y=(..count..)/sum(..count..))) + 
  geom_bar(aes(y = ..prop..)) +  theme_minimal() +
  geom_histogram(breaks=seq(0,100, by=5), 
                 col="#2166ac", 
                 fill="#2166ac", stat="bin") + xlim(0,100) +
  geom_vline(aes(xintercept = mean(charity_amount, na.rm=TRUE)),col='#b2182b',linetype="dotted",size=0.5, show.legend =TRUE)+
  labs(y="Proportion", x="Amount (US$)", title="Amount Donated to Charity, India") + theme(plot.title = element_text(hjust = 0.5))

### GGPLOT for US 


usplot <- ggplot(data=us, aes(x=charity_amount, y=(..count..)/sum(..count..))) + 
  geom_bar(aes(y = ..prop..)) +  theme_minimal() +
  geom_histogram(breaks=seq(0,100, by = 5), 
                 col="#2166ac", 
                 fill="#2166ac") + 
  geom_vline(aes(xintercept = mean(charity_amount, na.rm=TRUE)),col='#b2182b',linetype="dotted",size=0.5, show_guide=TRUE)+
  labs(y="Proportion", x="Amount (US$)", title="Amount Donated to Charity, U.S.") + theme(plot.title = element_text(hjust = 0.5))




### GGPLOT for Australia 


library(ggplot2)
library(scales)

ausplot <- ggplot(data=aus, aes(x=charity_amount, y=(..count..)/sum(..count..))) + 
  geom_bar(aes(y = ..prop..),alpha=0) +  theme_minimal() +
  geom_histogram(breaks=seq(0,100, by = 5), 
                 col="#2166ac", 
                 fill="#2166ac") + 
  geom_vline(aes(xintercept = mean(charity_amount, na.rm=TRUE)),col='#b2182b',linetype="dotted",size=0.5, show_guide=TRUE)+
  labs(y="Proportion", x="Amount (US$)", title="Amount Donated to Charity, Australia") + theme(plot.title = element_text(hjust = 0.5))


### Combine all plots together 


library(ggpubr)
figure <- ggarrange(ausplot, indplot, usplot,
                    ncol=3,nrow=1)

figure
ggsave("Figures/Amount Donated_Ult Game.jpg", width=10, height=6)




#### Appendix G: Table G1: Mean Donations in Ultimatum Game, by Incentive ####

# Standardize variable for treatment, then calculate by country

### Australia

aus$treatment <- ifelse(aus$consent==1,1,
                        ifelse(aus$consent==5, 2,
                               ifelse(aus$consent==6,3,
                                         ifelse(aus$consent==3,4,
                                                  ifelse(aus$consent==2,5,
                                                            ifelse(aus$consent==4,6,NA))))))


aus$treatment <- factor(aus$treatment, 
                        levels = c(1,2,3,4,5,6),
                        labels = c("Control", "$5 Charity", "$10 Charity", "Small Lottery", "Big Lottery", "Small+Big Lottery"))

library(dplyr)
library(magrittr)

aus_inc <- aus %>% 
  group_by(treatment) %>%
  summarise ("Mean(Aus)"=mean((charity_amount), na.rm=T),
             "SD(Aus)"=sd((charity_amount), na.rm=T))%>% 
  dplyr::mutate_if(is.numeric, round, 3)

print(aus_inc)

##### India ##### 


ind_ult$treatment <- factor(ind_ult$treatment, 
                        levels = c(1,2,3,4,5),
                        labels = c("Control", "Big Lottery", "Small Lottery", "Small+Big Lottery", "Charity"))


ind_ult_inct <- ind_ult %>%
  group_by(treatment) %>%
  dplyr::summarize("Mean(Ind)" = mean(charity_amount, na.rm=TRUE),
                   "SD(Ind)"= sd(charity_amount, na.rm=TRUE),
                   )

ind_ult_inct <- as.data.frame(ind_ult_inct) 
ind_ult_inct <- ind_ult_inct[c(4,1,NA, 5,2,3),]

print(ind_ult_inct)



### US

us$treatment <- ifelse(us$thankyou=="Thank you for completing the survey.",1,
                        ifelse(us$thankyou=="As a thank you for completing the survey, we will provide you with $5 to donate to a charitable organization of your choice.", 2,
                               ifelse(us$thankyou=="As a thank you for completing the survey, we will provide you with $10 to donate to a charitable organization of your choice.",3,
                                      ifelse(us$thankyou=="As a thank you for completing the survey, you will be entered into a lottery to win a cash prize (twenty USD 100 cash prizes).",4,
                                             ifelse(us$thankyou=="As a thank you for completing the survey, you will be entered into a lottery to win a cash prize (two USD 1000 cash prizes).",5,
                                                    ifelse(us$thankyou=="As a thank you for completing the survey, you will be entered into a lottery to win a cash prize (two USD 1000 cash prizes and twenty USD 100 cash prizes).",6,NA))))))


us$treatment <- factor(us$treatment, 
                        levels = c(1,2,3,4,5,6),
                        labels = c("Control", "$5 Charity", "$10 Charity", "Small Lottery", "Big Lottery", "Small+Big Lottery"))

library(dplyr)
library(magrittr)

us_inc <- us %>% 
  group_by(treatment) %>%
  summarise ("Mean(US)"=mean((charity_amount), na.rm=T),
             "SD(US)"=sd((charity_amount), na.rm=T))%>% 
  dplyr::mutate_if(is.numeric, round, 3)

us_inc <- us_inc[1:6,]

print(us_inc)

# Combine 3 data tables
options(digits=4)
tabg1 <- cbind(aus_inc, ind_ult_inct, us_inc)
keep <- c(1,2,3,5,6,8,9)
tabg1 <- tabg1[,keep]

tableg1 <- xtable::xtable(tabg1)
print(tableg1, file="Tables/tableg1.txt")

# Manually fix India tables so that Lottery conditions are in the right place; remove "treatment" column for paper

